package middleware

import (
	"bytes"
	"fmt"
	"github.com/gin-gonic/gin"
	"io"
)

func RequestLogger() gin.HandlerFunc {
	return func(c *gin.Context) {
		//// 打印请求方法和路径
		//fmt.Printf("请求: %s %s\n", c.Request.Method, c.Request.URL.Path)
		//
		//// 打印请求头
		//fmt.Println("请求头:")
		//for k, v := range c.Request.Header {
		//	fmt.Printf("%s: %v\n", k, v)
		//}

		// 打印请求体（仅对JSON请求）
		if c.ContentType() == "application/json" {
			rawData, _ := c.GetRawData()
			fmt.Printf("请求体: %s\n", string(rawData))
			c.Request.Body = io.NopCloser(bytes.NewBuffer(rawData)) // 重置
		}

		c.Next()
	}
}
